# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

source("misc.R")

library(combinat)

# count the number of motifs in the specified graph
#
# adj: adjacency matrix of the graph to be explored
# motif: adjacency matrix of the motif
count.motif <- function(adj, motif)
{
	n <- nrow(adj)
	s <- nrow(motif)
	
	# combination of s elements in [1, n]
	c <- combn(1:n, s)
	
	counter <- 0
	
	for (i in 1:ncol(c))
	{
		# select a column
		col <- c[,i]
		# take its first element
		f <- col[1]
		# get the permutation of the other elements
		p <- permn(col[2:length(col)])
		
		# for all permutations
		for (j in 1:length(p))
		{
			indices <- c(f, p[[j]])
			sub <- adj[indices, indices]
			comp <- g0 == sub
			fs <- length(which(comp == FALSE))
			if (fs == 0){ counter <- counter + 1 }
		}
	}
	
	counter
}